package conchmusic.conchmusicclient.thread;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import conchmusic.conchmusicclient.dao.ClientControl;
import conchmusic.conchmusicclient.dao.MyDBHelp;
import conchmusic.conchmusicclient.pojo.Song;

/**
 * Created by Administrator on 2017/4/23.
 */

public class OnlineThread implements Runnable {
    private String songUrl;
    private String pictureUrl;
    private String lyricUrl;
    private Song song;
    private Context context;
    private ClientControl clientControl;
    private MyDBHelp myDBHelp;
    private Handler handler;
    public OnlineThread(Song song , Context context , Handler handler){
        this.song=song;
        this.context=context;
        this.handler=handler;
    }
    @Override
    public void run() {
        try {
            myDBHelp=new MyDBHelp(context,"demon",null,1);
            clientControl=new ClientControl(myDBHelp);
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                songUrl=song.getSongUrl();
                songUrl.getBytes("UTF-8");
                URL URLSong=new URL(songUrl);
                InputStream inputSong=URLSong.openStream();
                String Downpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/DownloadMusic/";
                File fileDown = new File(Downpath);
                if(!fileDown.exists()){
                    fileDown.mkdirs();
                }
                String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/DownloadMusic/Music/";
                File file = new File(path);
                if(!file.exists()){
                    file.mkdirs();
                }
                File saveFile = new File(path+song.getTitle()+".mp3");
                FileOutputStream outStream = new FileOutputStream(saveFile);
                byte[] buff = new byte[1024];
                int sumByte=0;
                int hasRead = 0;
                while ((hasRead = inputSong.read(buff))!=-1) {
                    outStream.write(buff, 0, hasRead);
                    sumByte+=hasRead;
                }
                inputSong.close();
                outStream.close();
                pictureUrl=song.getPictureUrl();
                pictureUrl.getBytes("UTF-8");
                URL URLJGP=new URL(pictureUrl);
                InputStream inputSongJGP=URLJGP.openStream();
                String pathJPG=Environment.getExternalStorageDirectory().getAbsolutePath()+"/DownloadMusic/Picture/";
                File fileJPG = new File(pathJPG);
                if(!fileJPG.exists()){
                    fileJPG.mkdirs();
                }
                File saveFileJGP = new File(pathJPG+song.getTitle()+".JPG");
                FileOutputStream outStreamJGP = new FileOutputStream(saveFileJGP);
                byte[] buffJGP  = new byte[128];
                while ((hasRead = inputSongJGP.read(buffJGP))!=-1) {
                    outStreamJGP.write(buffJGP, 0, hasRead);
                }
                inputSongJGP.close();;
                outStreamJGP.close();

                lyricUrl=song.getLyricUrl();
                lyricUrl.getBytes("UTF-8");
                URL URLSongLrc=new URL(lyricUrl);
                InputStream inputSongLrc=URLSongLrc.openStream();
                String pathLrc=Environment.getExternalStorageDirectory().getAbsolutePath()+"/DownloadMusic/Lrc/";
                File fileLrc = new File(pathLrc);
                if(!fileLrc.exists()){
                    fileLrc.mkdirs();
                }
                File saveFileLrc = new File(pathLrc+song.getTitle()+".lrc");
                FileOutputStream outStreamLrc = new FileOutputStream(saveFileLrc);
                byte[] buffLrc = new byte[64];
                while ((hasRead = inputSongLrc.read(buffLrc))!=-1) {
                    outStreamLrc.write(buffLrc, 0, hasRead);
                }
                inputSongLrc.close();
                outStreamLrc.close();
                song.setLocalSongUrl(path+song.getTitle()+".mp3");
                song.setLocalPictureUrl(pathJPG+song.getTitle()+".JPG");
                song.setLocalLyricUrl(pathLrc+song.getTitle()+".lrc");
                clientControl.downLoadSong(song);
                Intent intent=new Intent();
                intent.putExtra("download","success");
                intent.setAction("conchmusic.conchmusicclient.downloadsuccess");
                context.sendBroadcast(intent);
                handler.sendEmptyMessage(4);
                Log.i("download","success");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();

            Log.i("OnlineThread112","no this url");
            return;
        } catch (IOException e) {
            e.printStackTrace();
            handler.sendEmptyMessage(5);
            Log.i("OnlineThread117","no this file");
            return;
        }
        //操作数据库
    }
}
